<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Model extends CI_Model {
	var $tableName;
	var $prefix;

	function __construct() {
		parent::__construct();
	}

	function insert($data) {

		if($this->tableName == "country" and !Common::isSuperAdmin()) {
			if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
			return FALSE;
		}
		if(Common::isAdmin()) {
			if($this->tableName == "city" and !Common::isAdminJog("COU", $data["city_cou_id"])) {
				if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
				return FALSE;
			}
			if($this->tableName == "institute" and !(Common::isAdminJog("INS") and (Common::isAdminJog("PAGE_CAT_IMP") or Common::isAdminJog("PAGE_CAT_FAC")))) {
				if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
				return FALSE;
			}
			if($this->tableName == "accomodation" and !(Common::isAdminJog("INS") and Common::isAdminJog("PAGE_CAT_ACC"))) {
				if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
				return FALSE;
			}
			if($this->tableName == "major" and !(Common::isAdminJog("INS", $data["maj_ins_id"]) and Common::isAdminJog("PAGE_CAT_MAJ"))) {
				if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
				return FALSE;
			}
			if($this->tableName == "scholarship" and !(Common::isAdminJog("PAGE_CAT_SCH"))) {
				if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
				return FALSE;
			}
		}
		
		/*
		if(in_array($this->tableName, array("continent", "country", "city", "institute", "major"))) {
			if(in_array($this->tableName, array("continent", "country", "city", "institute")) and !Common::isSuperAdmin()) {
				if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
				return FALSE;
			} elseif($this->tableName == "major" and !Common::isSuperAdmin()) {
				if(!isset($data["maj_ins_id"])) {
					if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
					return FALSE;
				}
				$this->db->select("*");
				$this->db->from("usr2ent");
				$this->db->where("u2e_usr_id", $this->session->userdata("usrId"));
				$this->db->where("u2e_ins_id", $data["maj_ins_id"]);
				$this->db->where("u2e_del", 0);
				$result = $this->db->get();
				if(!$result->num_rows()) {
					if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
					return FALSE;
				}
			}
		}
		*/

		foreach($data as $key => $val) {
			if(!is_null($val)) {
				$data[$key] = trim($val);
			}
		}

		if($this->db->insert($this->tableName, $data)) {
			$id = $this->db->insert_id();
			$log = array (
				"log_usr_id" => (($this->session->userdata("usrId"))?($this->session->userdata("usrId")):(NULL)),
				"log_table" => $this->tableName,
				"log_type" => "INS",
				"log_rec_id" => $id
			);
			$logChanges = array();
			foreach($data as $key => $value) {
				$logChanges[] = "<".$key.">".((is_null($value))?("NULL"):($value))."</".$key.">";
			}
			$log["log_changes"] = implode("", $logChanges);
			$this->db->set("log_date", "NOW()", FALSE);
			$this->db->insert("sys_log", $log);
			$this->load->model("sys_log");
			$this->sys_log->setSysCnt(TRUE);
			return $id;
		}
	}

	function update($id, $data) {
		$this->db->select("*");
		$this->db->from($this->tableName);
		$this->db->where($this->prefix."_id", $id);
		$result = $this->db->get();
		$old = $result->row();

		if(!defined("AUTH_OVERRIDE")) {
			if($this->tableName == "continent" and !Common::isSuperAdmin()) {
				if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
				return FALSE;
			}
			if($this->tableName == "country" and !Common::isAdminJog("COU", $old->cou_id)) {
				if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
				return FALSE;
			}
			if(Common::isAdmin()) {
				if($this->tableName == "city" and !(Common::isAdminJog("COU", $old->city_cou_id) or Common::isAdminJog("CITY", $old->city_id))) {
					if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
					return FALSE;
				}
				if($this->tableName == "institute" and !(Common::isAdminJog("INS", $old->ins_id) and (Common::isAdminJog("PAGE_CAT_IMP") or Common::isAdminJog("PAGE_CAT_FAC")))) {
					if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
					return FALSE;
				}
				if($this->tableName == "accomodation" and !(Common::isAdminJog("INS") and Common::isAdminJog("PAGE_CAT_ACC"))) {
					if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
					return FALSE;
				}
				if($this->tableName == "major" and !(Common::isAdminJog("INS", $old->maj_ins_id) and Common::isAdminJog("PAGE_CAT_MAJ"))) {
					if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
					return FALSE;
				}
				if($this->tableName == "scholarship" and !(Common::isAdminJog("PAGE_CAT_SCH"))) {
					if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
					return FALSE;
				}
			}
		}
		
		/*
		if(in_array($this->tableName, array("continent", "country", "city", "institute", "major"))) {
			if($this->tableName == "continent" and !Common::isSuperAdmin()) {
				if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
				return FALSE;
			} elseif($this->tableName == "major" and Common::isAdmin()) {
				$this->db->select("*");
				$this->db->from("usr2ent");
				$this->db->join("major", "usr2ent.u2e_ins_id = major.maj_ins_id and maj_del = 0 and maj_id = '".$this->db->escape_str($id)."'");
				$this->db->where("u2e_usr_id", $this->session->userdata("usrId"));
				$this->db->where("u2e_del", 0);
				$result = $this->db->get();
				if(!$result->num_rows()) {
					if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
					return FALSE;
				}
			} elseif($this->tableName == "institute" and Common::isAdmin()) {
				$this->db->select("*");
				$this->db->from("usr2ent");
				$this->db->join("institute", "usr2ent.u2e_ins_id = institute.ins_id and ins_del = 0 and ins_id = '".$this->db->escape_str($id)."'");
				$this->db->where("u2e_usr_id", $this->session->userdata("usrId"));
				$this->db->where("u2e_del", 0);
				$result = $this->db->get();
				if(!$result->num_rows()) {
					if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
					return FALSE;
				}
			} elseif($this->tableName == "city" and Common::isAdmin()) {
				$this->db->select("*");
				$this->db->from("usr2ent");
				$this->db->join("city", "usr2ent.u2e_city_id = city.city_id and city_del = 0 and city_id = '".$this->db->escape_str($id)."'");
				$this->db->where("u2e_usr_id", $this->session->userdata("usrId"));
				$this->db->where("u2e_del", 0);
				$result = $this->db->get();
				if(!$result->num_rows()) {
					// cou-n keresztul erjuk el
					$this->db->select("*");
					$this->db->from("usr2ent");
					$this->db->join("country", "usr2ent.u2e_cou_id = country.cou_id and cou_del = 0");
					$this->db->join("city", "country.cou_id = city.city_cou_id and city_del = 0 and city_id = '".$this->db->escape_str($id)."'");
					$this->db->where("u2e_usr_id", $this->session->userdata("usrId"));
					$this->db->where("u2e_del", 0);
					$result = $this->db->get();
					if(!$result->num_rows()) {
						if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
						return FALSE;
					}
				}
			} elseif($this->tableName == "country" and Common::isAdmin()) {
				$this->db->select("*");
				$this->db->from("usr2ent");
				$this->db->join("country", "usr2ent.u2e_cou_id = country.cou_id and cou_del = 0 and cou_id = '".$this->db->escape_str($id)."'");
				$this->db->where("u2e_usr_id", $this->session->userdata("usrId"));
				$this->db->where("u2e_del", 0);
				$result = $this->db->get();
				if(!$result->num_rows()) {
					if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
					return FALSE;
				}
			}
		}*/

		foreach($data as $key => $val) {
			if(!is_null($val)) {
				$data[$key] = trim($val);
			}
		}

		$changes = array();
		foreach($data as $key => $value) {
			if($old->$key != $value) {
				$changes[] = "<".$key.">".((is_null($value))?("NULL"):($value))."</".$key.">";
			}
		}

		if(!empty($changes)) {
			$this->db->where($this->prefix."_id", $id);

			if($this->db->update($this->tableName, $data)) {
				$log = array (
					"log_usr_id" => (($this->session->userdata("usrId"))?($this->session->userdata("usrId")):(NULL)),
					"log_table" => $this->tableName,
					"log_type" => "UPD",
					"log_rec_id" => $id
				);
				$log["log_changes"] = implode("", $changes);
				$this->db->set("log_date", "NOW()", FALSE);
				$this->db->insert("sys_log", $log);
				$this->load->model("sys_log");
				$this->sys_log->setSysCnt(TRUE);
			}
		}
	}

	function delete($id) {

		// update ellenőrzés copy
		$this->db->select("*");
		$this->db->from($this->tableName);
		$this->db->where($this->prefix."_id", $id);
		$result = $this->db->get();
		$old = $result->row();

		if($this->tableName == "continent" and !Common::isSuperAdmin()) {
			if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
			return FALSE;
		}
		if($this->tableName == "country" and !Common::isAdminJog("COU", $old->cou_id)) {
			if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
			return FALSE;
		}
		if(Common::isAdmin()) {
			if($this->tableName == "city" and !(Common::isAdminJog("COU", $old->city_cou_id) or Common::isAdminJog("CITY", $old->city_id))) {
				if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
				return FALSE;
			}
			if($this->tableName == "institute" and !Common::isAdminJog("INS", $old->ins_id)) {
				if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
				return FALSE;
			}
			if($this->tableName == "accomodation" and !(Common::isAdminJog("INS") and Common::isAdminJog("PAGE_CAT_ACC"))) {
				if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
				return FALSE;
			}
			if($this->tableName == "major" and !Common::isAdminJog("INS", $old->maj_ins_id)) {
				if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
				return FALSE;
			}
			if($this->tableName == "scholarship" and !Common::isAdminJog("PAGE_CAT_SCH")) {
				if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
				return FALSE;
			}
		}
		
		/*if(in_array($this->tableName, array("continent", "country", "city", "institute", "major"))) {
			if($this->tableName == "continent" and !Common::isSuperAdmin()) {
				if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
				return FALSE;
			} elseif($this->tableName == "major" and Common::isAdmin()) {
				$this->db->select("*");
				$this->db->from("usr2ent");
				$this->db->join("major", "usr2ent.u2e_ins_id = major.maj_ins_id and maj_del = 0 and maj_id = '".$this->db->escape_str($id)."'");
				$this->db->where("u2e_usr_id", $this->session->userdata("usrId"));
				$this->db->where("u2e_del", 0);
				$result = $this->db->get();
				if(!$result->num_rows()) {
					if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
					return FALSE;
				}
			} elseif($this->tableName == "institute" and Common::isAdmin()) {
				$this->db->select("*");
				$this->db->from("usr2ent");
				$this->db->join("institute", "usr2ent.u2e_ins_id = institute.ins_id and ins_del = 0 and ins_id = '".$this->db->escape_str($id)."'");
				$this->db->where("u2e_usr_id", $this->session->userdata("usrId"));
				$this->db->where("u2e_del", 0);
				$result = $this->db->get();
				if(!$result->num_rows()) {
					if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
					return FALSE;
				}
			} elseif($this->tableName == "city" and Common::isAdmin()) {
				$this->db->select("*");
				$this->db->from("usr2ent");
				$this->db->join("city", "usr2ent.u2e_city_id = city.city_id and city_del = 0 and city_id = '".$this->db->escape_str($id)."'");
				$this->db->where("u2e_usr_id", $this->session->userdata("usrId"));
				$this->db->where("u2e_del", 0);
				$result = $this->db->get();
				if(!$result->num_rows()) {
					// cou-n keresztul erjuk el
					$this->db->select("*");
					$this->db->from("usr2ent");
					$this->db->join("country", "usr2ent.u2e_cou_id = country.cou_id and cou_del = 0");
					$this->db->join("city", "country.cou_id = city.city_cou_id and city_del = 0 and city_id = '".$this->db->escape_str($id)."'");
					$this->db->where("u2e_usr_id", $this->session->userdata("usrId"));
					$this->db->where("u2e_del", 0);
					$result = $this->db->get();
					if(!$result->num_rows()) {
						if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
						return FALSE;
					}
				}
			} elseif($this->tableName == "country" and Common::isAdmin()) {
				$this->db->select("*");
				$this->db->from("usr2ent");
				$this->db->join("country", "usr2ent.u2e_cou_id = country.cou_id and cou_del = 0 and cou_id = '".$this->db->escape_str($id)."'");
				$this->db->where("u2e_usr_id", $this->session->userdata("usrId"));
				$this->db->where("u2e_del", 0);
				$result = $this->db->get();
				if(!$result->num_rows()) {
					if(!defined("AUTH_ERROR")) define("AUTH_ERROR", TRUE);
					return FALSE;
				}
			}
		}*/

		$this->db->where($this->prefix."_id", $id);
		if($this->db->update($this->tableName, array($this->prefix."_del" => time())) AND $this->db->affected_rows()) {
			$log = array (
				"log_usr_id" => (($this->session->userdata("usrId"))?($this->session->userdata("usrId")):(NULL)),
				"log_table" => $this->tableName,
				"log_type" => "DEL",
				"log_rec_id" => $id
			);
			$this->db->set("log_date", "NOW()", FALSE);
			$this->db->insert("sys_log", $log);
			$this->load->model("sys_log");
			$this->sys_log->setSysCnt(TRUE);
		}
	}

	function _presenter() {
		return false;
		$this->load->model("country");
		$query = "update ".$this->db->dbprefix."country left join ".$this->db->dbprefix."city on cou_id = city_cou_id and city_del = 0 left join ".$this->db->dbprefix."institute on city_id = ins_city_id and ins_del = 0 left join ".$this->db->dbprefix."ent2cat on e2c_entity = 'INS' and e2c_ent_id = ins_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set cou_presenter = if(e2c_id is not null, 'I', 'N')";
		$this->db->query($query);
		
		$this->load->model("city");
		$query = "update ".$this->db->dbprefix."city left join ".$this->db->dbprefix."country on cou_id = city_cou_id and cou_del = 0 and cou_presenter = 'I' left join ".$this->db->dbprefix."ent2cat on e2c_entity = 'CITY' and e2c_ent_id = city_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set city_presenter = if(e2c_id is not null, 'I', 'N')";
		$this->db->query($query);
		
		$this->load->model("institute");
		$query = "update ".$this->db->dbprefix."institute left join ".$this->db->dbprefix."city on city_id = ins_city_id and city_del = 0 and city_presenter = 'I' left join ".$this->db->dbprefix."ent2cat on e2c_entity = 'INS' and e2c_ent_id = ins_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set ins_presenter = if(e2c_id is not null, 'I', 'N')";
		$this->db->query($query);
		
		$this->load->model("major");
		$query = "update ".$this->db->dbprefix."major left join ".$this->db->dbprefix."institute on ins_id = maj_ins_id and ins_del = 0 and ins_presenter = 'I' left join ".$this->db->dbprefix."ent2cat on e2c_entity = 'MAJ' and e2c_ent_id = maj_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set maj_presenter = if(e2c_id is not null, 'I', 'N')";
		$this->db->query($query);
		
		$this->load->model("scholarship");
		$query = "update ".$this->db->dbprefix."scholarship left join ".$this->db->dbprefix."ent2cat on e2c_entity = 'SCH' and e2c_ent_id = sch_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set sch_presenter = if(e2c_id is not null, 'I', 'N')";
		$this->db->query($query);
		
	}

	function select($select = '*', $escape = NULL) {
		$this->db->select($select, $escape);
		return $this;
	}

	function where($field, $value = null) {
		if(is_null($value)) {
			$this->db->where($field, null, FALSE);
		} else {
			$this->db->where($field, $value);
		}
		return $this;
	}

	function first($field = null, $value = null) {
		// TODO null esetet kezelni
		return $this->firstOrDefault($field, $value);
	}

	function firstOrDefault($field = null, $value = null) {
		if(!is_null($field)) {
			$this->where($field, $value);
		}
		$this->db->where($this->prefix."_del", 0);
		$ret = $this->db->get($this->tableName)->result();

		if($ret) {
			if(isset($this->db->joined)) {
				$r = new stdClass();
				foreach($ret[0] as $key => $value) {
					$fieldPrefix = explode("_", $key);
					$fieldPrefix = $fieldPrefix[0];
					$r->$key = $value;
				}
				return $r;
			}
			return $ret[0];
		}
		return null;
	}

	function getById($id) {
		return $this->firstOrDefault($this->prefix."_id", $id);
	}

	function orderBy($orderBy = null, $orderHow = null) {
		if($orderBy) {
			$this->db->order_by($orderBy, is_null($orderHow) ? "" : $orderHow);
		}
		return $this;
	}

	function groupBy($groupBy) {
		$this->db->group_by($groupBy);
		return $this;
	}

	function getAll($field = null, $value = null) {
		if(!is_null($field)) {
			$this->where($field, $value);
		}
		$this->db->where($this->prefix."_del", 0);
		return $this->db->get($this->tableName)->result();
	}

	function count($field = null, $value = null) {
		if(!is_null($field)) {
			$this->where($field, $value);
		}
		$this->db->where($this->prefix."_del", 0);
		return $this->db->count_all_results($this->tableName);
	}

}

?>